package com.neu.flashmall.product.controller;

import com.neu.common.entity.Tree;
import com.neu.common.utils.ValidUtil;
import com.neu.common.valid.AddGroup;
import com.neu.common.valid.UpdateGroup;
import com.neu.flashmall.product.entity.PmsCategory;
import com.neu.flashmall.product.service.PmsCategoryService;
import com.neu.common.utils.Result;
import com.neu.flashmall.product.vo.HeaderCategoryVo;
import com.neu.flashmall.product.vo.PmsCategoryVo;
import com.neu.flashmall.product.vo.PmsCategoryMallVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 50286
 *
 */
@RestController
@RequestMapping(value = "/pms_category")
@Api(value = "商品类别管理接口")
public class PmsCategoryController {

    @Resource
    PmsCategoryService pmsCategoryService;

    @GetMapping(value = "/getAllCategory")
    @ApiOperation(value = "获取所有的商品类别信息")
    public Result getAllCategory(){
        PmsCategoryVo pmsCategoryVo = pmsCategoryService.getPmsCategoryVo();
        return Result.ok().put(pmsCategoryVo);
    }

    @GetMapping(value = "/getAllThirdCategory")
    @ApiOperation(value = "获取所有的三级商品类别信息")
    public Result getAllThirdCategory(){
        List<PmsCategory> list = pmsCategoryService.getAllThirdCategory();
        return Result.ok().put(list);
    }

    @GetMapping(value = "/getPmsCategoryMallVo")
    @ApiOperation(value = "获取二级分类下的商品推荐")
    public Result getPmsCategoryMallVo(Long id){
        PmsCategoryMallVo pmsCategoryMallVo = pmsCategoryService.makeUpPmsCategoryMallVo(id);
        return Result.ok().put(pmsCategoryMallVo);
    }

    @PutMapping(value = "/check/addCategoryLevel")
    @ApiOperation(value = "添加一级商品类别")
    public Result addCategoryLevel1(
            @RequestBody @Validated(AddGroup.class) PmsCategory pmsCategory,
            BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ValidUtil.createResultByBindingResult(bindingResult);
        }
        String outcome = this.pmsCategoryService.addPmsCategoryLevel1(pmsCategory);
        if(outcome.equals("添加商品类别成功")){
            return Result.ok().setMessage(outcome).put(pmsCategory.getId());
        }
        return Result.error().setMessage(outcome);
    }

    @PostMapping(value = "/check/updateCategory")
    @ApiOperation(value = "更新类别信息接口")
    public Result updateCategory(
            @RequestBody @Validated(value = UpdateGroup.class) PmsCategory pmsCategory,
            BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ValidUtil.createResultByBindingResult(bindingResult);
        }
        String outcome = this.pmsCategoryService.updatePmsCategory(pmsCategory);
        if(outcome.equals("更新类别信息成功")){
            return Result.ok().setMessage(outcome);
        }
        return Result.error().setMessage(outcome);
    }

    @DeleteMapping(value = "/check/deleteCategory")
    @ApiOperation(value = "删除类别接口")
    public Result deleteCategory(Long id){
        String outcome = this.pmsCategoryService.deletePmsCategory(id);
        if(outcome.equals("删除类别成功")){
            return Result.ok().setMessage(outcome);
        }
        return Result.error().setMessage(outcome);
    }

    @PutMapping(value = "/check/updateIcon")
    @ApiOperation(value = "更新类别URL接口")
    public Result updateIcon(@RequestBody PmsCategory pmsCategory){
        String outcome = this.pmsCategoryService.updateIcon(pmsCategory);
        if(outcome.equals("更新URL成功")){
            return Result.ok().setMessage(outcome);
        }
        return Result.error().setMessage(outcome);
    }

    @GetMapping(value = "/getHeaderCategory")
    @ApiOperation(value = "获取头部二级分类数据")
    public Result getHeaderCategory(int quantity,int childrenQuantity){
        HeaderCategoryVo result = this.pmsCategoryService.makeUpHeaderCategoryVo(quantity, childrenQuantity);
        return Result.ok().put(result);
    }

    @GetMapping(value = "/getThirdCategory")
    @ApiOperation(value = "获得等级三的类别list")
    public Result getThirdCategory(){
        List<PmsCategory> result = this.pmsCategoryService.getThirdCategory();
        if(result == null){
            return Result.error();
        }
        return Result.ok().put(result);
    }

    @GetMapping(value = "/getCategoryTree")
    @ApiOperation("获得类型的树结构数据,不含Category的完整数据，只含有id和name")
    public Result getCategoryTree(){
        Tree tree = this.pmsCategoryService.getCategoryTree();
        if(tree != null){
            return Result.ok().put(tree);
        }
        return Result.error();
    }
}
